package com.xiaoyi.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

/**
 * @author xiaoyi
 * @version 1.0.0
 * @date 2023/08/01 16:37:00
 */
public class Base64Util {

    /**
     * 对给定的字符串进行base64解码操作
     */
    public static String decrypt(String inputData) {
        if (null == inputData) {
            return null;
        }
        byte[] b = null;
        String result = null;

        BASE64Decoder decoder = new BASE64Decoder();
        try {
            b = decoder.decodeBuffer(inputData);
            result = new String(b, StandardCharsets.UTF_8);
        } catch (Exception e) {
            result = "";
        }

        return result;


    }

    /**
     * 对给定的字符串进行base64加密操作
     */
    public static String encrypt(String inputData) {
        if (null == inputData) {
            return null;
        }
        byte[] b = null;
        String s = null;
        b = inputData.getBytes(StandardCharsets.UTF_8);
        if (b != null) {
            s = new BASE64Encoder().encode(b);
        }
        return s;
    }

}
